package view 
{
	import core.ViewBase;
	import data.Chapter;
	import data.Lesson;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import service.MusicService;
	import ui.UIPorgressBar;
	import util.NumberFormat;
	
	/**
	 * ...
	 * @author liwei
	 */
	public class SkinView extends ViewBase 
	{
		[ui]public var prevBtn:SimpleButton;
		[ui]public var nextBtn:SimpleButton;
		[ui]public var playBtn:SimpleButton;
		[ui]public var pauseBtn:SimpleButton;
		[ui]public var menuBtn:SimpleButton;
		[ui]public var fullBtn:SimpleButton;
		[ui]public var unfullBtn:SimpleButton;
		[ui]public var helpBtn:SimpleButton;
		[ui]public var exitBtn:SimpleButton;
		[ui]public var progressbarBack:Sprite;
		[ui]public var progressbarBtn:Sprite;
		[ui]public var soundBack:Sprite;
		[ui]public var soundBtn:Sprite;
		[ui]public var musicBack:Sprite;
		[ui]public var musicBtn:Sprite;
		[ui]public var timeTxt:TextField;
		[ui]public var title_txt:TextField;
		////
		[Inject]
		public var music:MusicService;
		private var pro:UIPorgressBar;
		private var soundpro:UIPorgressBar;
		private var musicpro:UIPorgressBar;
		public var position:Number;
		public function SkinView() 
		{
			super();
			load("swf/skin.swf");
		}
		override protected function loadInit():void 
		{
			pro = new UIPorgressBar();
			progressbarBack.alpha = 0;
			pro.setSkin(progressbarBack, progressbarBtn);
			pro.addEventListener(ProgressEvent.PROGRESS, pro_progress);
			
			soundpro = new UIPorgressBar();
			soundpro.setSkin(soundBack, soundBtn);
			soundpro.setProgress(0.8);
			soundpro.addEventListener(ProgressEvent.PROGRESS, soundpro_progress);
			
			musicpro = new UIPorgressBar();
			musicpro.setSkin(musicBack, musicBtn);
			musicpro.setProgress(0);
			music.setVolume(0);
			musicpro.addEventListener(ProgressEvent.PROGRESS, musicpro_progress);
			
			//musicBack.visible = musicBtn.visible = false;
			
			unfullBtn.visible = false;
		}
		public function startLesson():void {
			pro.enable = State.control;
			timeTxt.visible = State.control;
			var chapter:Chapter = Chapter.getChapter(State.chapter);
			var msg:String = chapter.name;
			var lesson:Lesson = Chapter.getLessonByFlag(State.chapter, State.lesson);
			if (lesson != null) {
				if (lesson.name != "") {
					msg += " / " + lesson.name;
				}
			}
			if (title_txt != null) {
				title_txt.text = msg;
			}
		}
		public function update():void {
			var pos:Number = 0;
			var fps:Number = State.fps;
			var tf:int = State.totalFrame;
			var cf:int = State.currentFrame;
			pos = cf / tf;
			pro.setProgress(pos);
			timeTxt.text = NumberFormat.formatTime(tf * pos / fps * 1000) + ":" + NumberFormat.formatTime(tf / fps * 1000);
			pauseBtn.visible = !State.pause;
		}
		private function pro_progress(e:ProgressEvent):void 
		{
			position = e.bytesLoaded / e.bytesTotal;
			dispatchEvent(new Event("setposition"));
			State.positioning = true;
			if (!pro.draging) {
				State.positioning = false;
				dispatchEvent(new Event("endposition"));
			}
			//trace(e.bytesLoaded+"/"+e.bytesTotal);
		}
		private function soundpro_progress(e:ProgressEvent):void 
		{
			music.setMovieVolume(e.bytesLoaded / e.bytesTotal);
		}
		
		private function musicpro_progress(e:ProgressEvent):void 
		{
			music.setVolume(e.bytesLoaded / e.bytesTotal);
		}
	}

}